// src/api/reminders.ts

import API_BASE_URL from '../core/config'
import axios from 'axios'
import { applyInterceptors } from './axiosInstance';
import type { Reminder } from '../types/reminder'; // 假设有对应的类型定义

// 创建 axios 实例
const api = axios.create({
    baseURL: API_BASE_URL,
    timeout: 10000,
});

applyInterceptors(api);

export const remindersApi = {
    /**
     * 获取分页提醒列表
     * @param page 当前页码，从1开始
     * @param limit 每页数量
     */
    async getReminders(page = 1, limit = 10) {
        try {
            const response = await api.get('/reminders/', {
                params: { page, limit },
            });
            return {
                items: response.data.items,
                total: response.data.total,
                page: response.data.page,
                pageSize: response.data.page_size
            };
        } catch (error) {
            console.error('获取提醒列表失败:', error);
            throw error;
        }
    },

    /**
     * 获取单个提醒详情
     * @param id 提醒ID
     */
    async getReminder(id: number): Promise<Reminder> {
        try {
            const response = await api.get(`/reminders/${id}`);
            return response.data;
        } catch (error) {
            console.error('获取提醒详情失败:', error);
            throw error;
        }
    },

    /**
     * 创建新提醒
     * @param reminder 提醒数据
     */
    async createReminder(reminder: Reminder): Promise<Reminder> {
        try {
            console.log('创建提醒:', reminder);
            const response = await api.post('/reminders/', reminder);
            return response.data;
        } catch (error) {
            console.error('创建提醒失败:', error);
            throw error;
        }
    },

    /**
     * 更新提醒
     * @param id 提醒ID
     * @param reminder 更新的提醒数据
     */
    async updateReminder(id: number, reminder: Partial<Reminder>): Promise<Reminder> {
        try {
            const response = await api.put(`/reminders/${id}`, reminder);
            return response.data;
        } catch (error) {
            console.error('更新提醒失败:', error);
            throw error;
        }
    },

    /**
     * 切换提醒激活状态
     * @param id 提醒ID
     */
    async toggleReminder(id: number): Promise<Reminder> {
        try {
            const response = await api.put(`/reminders/${id}/toggle`);
            return response.data;
        } catch (error) {
            console.error('切换提醒状态失败:', error);
            throw error;
        }
    },

    /**
     * 删除提醒
     * @param id 提醒ID
     */
    async deleteReminder(id: number): Promise<void> {
        try {
            await api.delete(`/reminders/${id}`);
        } catch (error) {
            console.error('删除提醒失败:', error);
            throw error;
        }
    }
}